package com.example.config;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class SentinelResourceInitializer {

    @PostConstruct
    public void init() {
        // 预加载所有资源
        preloadResource("flowControlResource");
        preloadResource("circuitBreakerResource");
        preloadResource("hotSpotResource");
        preloadResource("systemRuleResource");
        preloadResource("authorityRuleResource");
    }

    private void preloadResource(String resourceName) {
        try (Entry entry = SphU.entry(resourceName)) {
            // 这里无需写入实际逻辑，只是确保资源被 Sentinel 注册
        } catch (BlockException ex) {
            // 处理被阻止的异常，这里可以留空，因为我们只关注资源注册
        }
    }
}
